//
//  ViewController.swift
//  MSShimmer
//
//  Created by huizhou.wang on 07/23/2025.
//  Copyright (c) 2025 huizhou.wang. All rights reserved.
//

import UIKit
import MSShimmer
import SnapKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        print("==================ViewController")
        
//        MSShimmerAppearance.instance.shimmerCornerRadius = 6
//        MSShimmerAppearance.instance.duration = 1.5
//        
//        MSShimmerAppearance.instance.shimmerColors = [
//            UIColor(red: 0, green: 0, blue: 0, alpha: 0.102).cgColor,
//            UIColor(red: 0, green: 0, blue: 0, alpha: 0.3).cgColor,
//            UIColor(red: 0, green: 0, blue: 0, alpha: 0.102).cgColor
//        ]
        
        view.addSubview(shimmer)
        shimmer.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(100)
            make.centerX.equalToSuperview()
            make.width.equalTo(200)
            make.height.equalTo(12)
        }
        
        view.addSubview(shimmer2)
        shimmer2.snp.makeConstraints { make in
            make.top.equalTo(shimmer.snp.bottom).offset(10)
            make.centerX.equalToSuperview()
            make.width.equalTo(200)
            make.height.equalTo(12)
        }
        
        view.addSubview(shimmer3)
        shimmer3.snp.makeConstraints { make in
            make.top.equalTo(shimmer2.snp.bottom).offset(10)
            make.centerX.equalToSuperview()
            make.width.equalTo(200)
            make.height.equalTo(12)
        }
        
        
        view.addSubview(contextView)
        contextView.snp.makeConstraints { make in
            make.top.equalTo(shimmer3.snp.bottom).offset(20)
            make.leading.trailing.equalToSuperview().inset(40)
            make.height.equalTo(200)
        }
        
        contextView.addSubview(shimmer4)
        shimmer4.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(20)
            make.leading.trailing.equalToSuperview().inset(40)
            make.height.equalTo(12)
        }
        
        //=================================
        view.addSubview(shimmerBoard)
        shimmerBoard.snp.makeConstraints { make in
            make.top.equalTo(contextView.snp.bottom).offset(20)
            make.leading.trailing.equalToSuperview().inset(40)
            make.height.equalTo(200)
        }
        
        shimmerBoard.addSubview(shimmerB1)
        shimmerB1.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(20)
            make.leading.trailing.equalToSuperview().inset(40)
            make.height.equalTo(12)
        }
        
        shimmerBoard.addSubview(shimmerB2)
        shimmerB2.snp.makeConstraints { make in
            make.top.equalTo(shimmerB1.snp.bottom).offset(20)
            make.leading.trailing.equalToSuperview().inset(40)
            make.height.equalTo(12)
        }
        
        
//        DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
            print("============start")
            self.start()
//        }
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
            print("============stop")
//            self.stop()
        }
        
    }
    
    
    
    lazy var shimmer: MSShimmerContainerView = {
        let it = MSShimmerContainerView()
        return it
    }()
    
    lazy var shimmer2: MSShimmerContainerView = {
        let it = MSShimmerContainerView()
        return it
    }()
    
    lazy var shimmer3: MSShimmerContainerView = {
        let it = MSShimmerContainerView()
        return it
    }()
    
    lazy var contextView: MSShimmerContextView = {
        let it = MSShimmerContextView()
        it.backgroundColor = UIColor.gray.withAlphaComponent(0.3)
        return it
    }()
    
    lazy var shimmer4: MSShimmerContainerView = {
        let it = MSShimmerContainerView()
        return it
    }()
    
    
    lazy var shimmerBoard: MSShimmerBoardContainerView = {
        let it = MSShimmerBoardContainerView()
        it.backgroundColor = UIColor.gray.withAlphaComponent(0.3)
        return it
    }()
    
    lazy var shimmerB1: MSShimmerContainerView = {
        let it = MSShimmerContainerView()
        return it
    }()
    
    lazy var shimmerB2: MSShimmerContainerView = {
        let it = MSShimmerContainerView()
        return it
    }()
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("===================")
    }
}

extension ViewController:MSShimmerSource {
    var shimmerContainers: [MSShimmerContainerView] {
        return [
            shimmer,
            shimmer2,
            shimmer3,
            contextView,
            shimmer4,
            shimmerBoard
        ]
    }
}

